package edu.cuit.zhuyimeng.framework.security.config;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.SaTokenDaoRedissonJackson;
import edu.cuit.zhuyimeng.framework.cache.config.CacheAutoConfiguration;
import edu.cuit.zhuyimeng.framework.security.proxy.SaTokenDaoProxy;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;

import java.lang.reflect.Proxy;

@AutoConfigureAfter(CacheAutoConfiguration.class)
@AutoConfiguration
@RequiredArgsConstructor
public class TokenDaoAutoConfiguration {

    private final RedissonClient redissonClient;

    @Bean
    public SaTokenDao saTokenDao() {
        Class<SaTokenDaoRedissonJackson> redissonJacksonClass = SaTokenDaoRedissonJackson.class;
        SaTokenDao saTokenDaoProxyInstance = (SaTokenDao) Proxy.newProxyInstance(redissonJacksonClass.getClassLoader(), redissonJacksonClass.getInterfaces(), new SaTokenDaoProxy(redissonClient));
        SaManager.setSaTokenDao(saTokenDaoProxyInstance);
        return saTokenDaoProxyInstance;
    }


}
